﻿using IMModel.Domain.Common;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System;
namespace IMModel.Domain
{
    //class DefaultAnswerVolumePolicy : IAnswerVolumePolicy
    //{
    //    public static readonly DefaultAnswerVolumePolicy Instance = new DefaultAnswerVolumePolicy();


    //}

    /// <summary>
    /// Стратегия расчёта индексов по умолчанию
    /// </summary>
    public class DefaultAnswerVolumePolicy : IAnswerVolumePolicy
    {
        public static readonly DefaultAnswerVolumePolicy Instance = new DefaultAnswerVolumePolicy();

        /// <summary>
        /// Расчитывает индексы коллекции с элементами типа IRespondentIndexCollector: I' = Sum(I*W) / Max(I*W)
        /// </summary>
        /// <typeparam name="T">Тип элемента коллекции</typeparam>
        /// <param name="source">Коллекция индексов</param>
        /// <param name="weightFunc">определитель веса элемента</param>
        /// <returns>Коллекция индексов</returns>
        //public RespondentIndexCollection GetIndexes<T>(IndexContext ic,
        //    IEnumerable<T> source, Func<T, double> weightFunc)
        //    where T : IRespondentIndexVector
        //{
        //    Contract.Requires(source != null);
        //    Contract.Requires(weightFunc != null);

        //    RespondentIndexCollection result = null;
        //    foreach (var item in source)
        //    {
        //        result += item.CalcIndexes(ic) * weightFunc(item);// item.GetIndexes(weightFunc(item));
        //    }

        //    return (result != null) ? (result / result.MaxValue) : null;
        //}

        public RespondentIndexCollection GetIndexes(IndexContext сontext, 
            CharacteristicTerm currCharactericTerm)
        {
            Contract.Requires(currCharactericTerm != null);
            var term = сontext.Term; //currCharactericTerm.Term;
            var c = new RespondentIndexCollection();
            while (currCharactericTerm != null)
            {
                if (currCharactericTerm.IsValidInPeriod(term))
                {
                    c += currCharactericTerm.GetTermVolumes(сontext, currCharactericTerm, term);
                }
                currCharactericTerm = currCharactericTerm.InsteadOf;
            }
            return c;
        }
    }

    /// <summary>
    /// Стратегия расчёта индексов для докторов наук
    /// </summary>
    public class DoctorAnswerVolumePolicy : IAnswerVolumePolicy
    {
        public static readonly DoctorAnswerVolumePolicy Instance = new DoctorAnswerVolumePolicy();

        /// <summary>
        /// Расчитывает индексы коллекции с элементами типа IRespondentIndexCollector: I' = Sum(I*W) / Max(I*W)
        /// </summary>
        /// <typeparam name="T">Тип элемента коллекции</typeparam>
        /// <param name="source">Коллекция индексов</param>
        /// <param name="weightFunc">определитель веса элемента</param>
        /// <returns>Коллекция индексов</returns>
        public RespondentIndexCollection GetIndexes(IndexContext сontext,
            CharacteristicTerm currCharactericTerm)
        {
            Contract.Requires(currCharactericTerm != null);
            var term = сontext.Term; //currCharactericTerm.Term;
            var c = new RespondentIndexCollection();
            while (currCharactericTerm != null)
            {
                if (currCharactericTerm.IsValidInPeriod(term))
                {
                    c += currCharactericTerm.GetTermVolumes(сontext, currCharactericTerm, term);
                }
                currCharactericTerm = currCharactericTerm.InsteadOf;
            }
            return c;
        }

        //public RespondentIndexCollection GetIndexes<T>(IndexContext сontext,
        //    IEnumerable<T> source, Func<T, double> weightFunc)
        //    where T : IRespondentIndexVector
        //{
        //    Contract.Requires(source != null);
        //    Contract.Requires(weightFunc != null);

        //    RespondentIndexCollection result = null;
        //    foreach (var item in source)
        //    {
        //        result += item.CalcIndexes(сontext) * weightFunc(item); // item.GetIndexes(weightFunc(item));
        //    }
        //    return (result != null) ? (result / result.MaxValue) : null;
        //}
    }
}